package com.dmbjz.rpc.service;


import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeatherService {

    @Resource
    private WeatherInterface weatherApi;


    /**
     * 使用响应式编程的 WebClient 进行请求
     */
    public Mono<String> weather(String city) {

        //创建请求参数
        Map<String, String> paramMap = new HashMap<>(1);
        paramMap.put("area", city);


        //创建WebClient对象
        WebClient client = WebClient.create();

        //定义请求行为
        Mono<String> result = client.get()
                //定义请求路径、请求参数
                .uri("https://ali-weather.showapi.com/area-to-id?area={area}", paramMap)
                //定义响应内容类型
                .accept(MediaType.APPLICATION_JSON)
                //定义请求头
                .header("Authorization", "APPCODE 27bb229111ad424abd3558998da8be9a")
                //发起请求
                .retrieve()
                //解析结果
                .bodyToMono(String.class);

        return result;

    }


    /**
     * 使用 HttpInterface 代理对象方式 进行请求
     */
    public Mono<String> weather2(String city) {

        return weatherApi.getWeather(city, "APPCODE 27bb229111ad424abd3558998da8be9a");

    }


}
